// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        arrow: cc.Node,
        body: cc.Node,
        attackCircle: cc.Node,

    },
    setMoveDirection(dir) {
        this.moveDir = dir;
    },
    // LIFE-CYCLE CALLBACKS:
    syncMove() {
        if (this.moveDir == null) {
            return;
        }
        let angelHD = this.moveDir * Math.PI / 180;
        let dx = Math.sin(angelHD) * this.speed;
        let dy = Math.cos(angelHD) * this.speed;
        if (this.node.x + dx < -1500 || this.node.x + dx > 750) {
            dx = 0;
        }
        if (this.node.y + dy <= -2300 || this.node.y + dy >= 1000) {
            dy = 0;
        }
        this.node.x += dx;
        this.node.y += dy;
        this.arrow.angle = -this.moveDir;
        this.body.angle = -this.moveDir;
    },
    synBodyDirection() {
        let monster = null;
        if (this.fightgole) {
            //区分一下大怪和小怪
            if (this.fightgole < 1000) {
                //怎么获取孩子怪物呢
                let momList = g_Monster.getAllMom();
                for (let momid in momList) {
                    let mom = momList[momid];
                    for (let cID in mom.data) {
                        if (cID == this.fightgole) {
                            monster = mom.data[cID];
                            continue;
                        }
                    }
                    if (monster != null) {
                        break;
                    }
                }
            }
            else {
                monster = g_Monster.getMonsterByUid(this.fightgole);
            }
            if (!monster) {
                return;
            }
            let x = monster.x - this.node.x;
            let y = monster.y - this.node.y;
            if (y == 0) {
                y = 1;
            }
            let angle = Math.atan(x / y) * 180 / Math.PI;
            if (y < 0) {
                angle += 180;
            }
            this.body.angle = -angle;

        }
    },
    onCollisionEnter(other, self) {
        if (other.node.group == "decorate") {
            other.node.opacity = 100;
        } else if (other.node.group == "monster") {
            this.hurtList[other.node.ID] = {};
        }
    },
    onCollisionExit(other, self) {
        if (other.node.group == "decorate") {
            other.node.opacity = 255;
        } else if (other.node.group == "monster") {
            delete this.hurtList[other.node.ID];
        }
    },
    chickEnemy() {
        //哎哟这里还要查找他喵的孩子怪物
        let monsterlist = g_Monster.getAllMonster();
        let tMonList = monsterlist;
        for (let id in monsterlist) {
            //拼接孩子怪物
            let monster = monsterlist[id];
            if (!monster.Cid) {
                continue;
            }
            //该怪物为母亲怪物
            //拼接对象
            tMonList = { ...monsterlist, ...monster.data };
        }
        monsterlist = tMonList;
        let gole = null;
        let minDis = null;
        for (let uid in monsterlist) {
            let monster = monsterlist[uid];
            let dis = (monster.x - this.node.x) ** 2 + (monster.y - this.node.y) ** 2;
            if (minDis == null || dis < minDis) {
                minDis = dis;
                gole = uid;
            }
        }
        if (minDis && minDis < this.circleLength ** 2) {
            this.fightgole = gole;
        }
        else {
            this.fightgole = null;
        }

    },
    chickAttack(dt) {
        //有保护时间的时候不会调用伤害函数
        if (this.protectByTime > 0) {
            this.protectShow.fillRange = this.protectByTime / this.protectTime;
            this.protectByTime -= dt;
            return;
        }
        //但是怎样不让保护时间为0呢
        //这里的代码都Time小于0
        //通过圆环显示剩余保护时间
        let tLst = {};
        let hurt = 0;
        for (let id in this.hurtList) {
            let info = this.hurtList[id];
            let tMonster = g_Monster.getMonsterByUid(id);
            if (!tMonster) {
                continue;
            }
            //统计最大的伤害值
            if (hurt < tMonster.attack) {
                hurt = tMonster.attack;
            }
            tLst[id] = info;
            if (hurt != 0) {
                this.protectByTime = this.protectTime;
                this.onHurt(hurt);
            }
        }
        this.hurtList = tLst;
    },
    showHP() {
        this.hpLb.string = this.hp + "/" + this.hpMax;
        this.hpBar.progress = this.hp / this.hpMax;
    },

    synStop() {
        this.moveDir = null;
    },
    onLoad() {
        this.node.script = this;
    },
    getBodyAngle() {
        return this.body.angle;
    },
    start() {
        this.moveDir = null;
        this.circleLength = 300;//射击范围
        this.fightgole = null;
        this.launchTime = 0;
        this.launchSpeed = 0.2;
        this.attack = 20;
        this.launchSpRatio = 1;
        this.launchNum = 1;//子弹数目
        this.speed = 8;//角色一栋速度
        // this.buff = 1;
        this.poison = 0;//角色拥有毒buff
        this.fire = 0;//角色拥有火焰弹buff
        this.critChance = 20;//暴击率
        this.critRate = 150;//暴击倍率
        this.leech = 0;//吸血的倍率
        this.hurtList = {};

        this.hpMax = 300;
        this.hp = 300;
        this.hpBar = cc.find("Canvas/gamepanel/role/hpbg/rolebattlebg2").getComponent(cc.ProgressBar);
        this.hpLb = cc.find("Canvas/gamepanel/role/hpbg/hpLabel").getComponent(cc.Label);
        this.protectShow = cc.find("Canvas/gamepanel/role/arrow/role_battle_green").getComponent(cc.Sprite)//无敌时间的显示
        this.hpBar.progress = 1;
        this.protectTime = 0.5;//最大的保护时间
        this.protectByTime = 0.5;//剩余的保护时间
    },
    getAttack() {
        return this.attack;
    },
    getLaunchNum() {
        return this.launchNum;
    },
    addLaunchNum(num) {
        return this.launchNum++;
    },
    addCritChance(chance) {//增加暴击率，上限为100
        if (this.critChance + chance <= 100) {
            this.critChance += chance;
        } else {
            this.CritChance = 100;
        }
    },
    addCritRate(rate) {
        this.critRate += rate;
    },
    getCritChance() {
        return this.critChance;
    },
    getCritRate() {
        return this.critRate;
    },
    addCircleLength(length) {
        this.circleLength += length;
        this.attackCircle.setContentSize(this.circleLength * 2, this.circleLength * 2);
    },
    addLeech(num) {
        this.leech += num;
    },
    getLeech() {
        return this.leech;
    },
    addSpeed(speedTimes) {
        this.speed = this.speed * (100 + speedTimes) / 100;
    },
    doLeech(hurt) {
        //开始吸血，中毒灼烧伤害也能吸血
        if (this.hp == this.hpMax || this.leech == 0) {
            //满血、无吸血不处理
            return
        }
        let ttHealing = this.leech / 100 * hurt;//治疗量
        if (this.hp + ttHealing > this.hpMax) {
            this.hp = this.hpMax;
        } else {
            this.hp += ttHealing;
        }
    },
    doLaunch(dt) {

        this.launchTime += dt;
        if (this.launchTime < this.launchSpeed / this.launchSpRatio) {
            return;
        } else {
            this.launchTime -= this.launchSpeed / this.launchSpRatio;
        }
        if (!this.fightgole) {
            return;
        }
        g_Bullet.doLaunch();
    },
    addSpeedBySkill(speed) {
        this.launchSpRatio += speed;
        this.launchTime = 0;
        if (this.launchSpRatio > 2) {
            this.launchSpRatio = 2;
        }
    },
    addABS(attack) {
        this.attack += attack;
    },
    addPoisonBuff(buffNum) {
        this.buff = buffNum;
    },
    addPoison() {
        this.poison++;
    },
    getPoison() {
        return this.poison;
    },
    addFire() {
        this.fire++;
    },
    getFire() {
        return this.fire;
    },
    // getRoleBuff() {
    //     return this.buff;
    // },
    onHurt(hurt) {
        g_Hurt.showHurtLabel(this.node, hurt, 3);
        this.hp -= hurt;
        if (this.hp <= 0) {
            game.gameOver();
            return;
        }

    },
    update(dt) {
        if (game.pause) {
            return;
        }
        this.syncMove();
        this.chickEnemy();
        this.synBodyDirection();
        this.doLaunch(dt);
        this.chickAttack(dt);
        this.showHP();//时刻展示血量
        if (this.fightgole) {
            g_Bullet.update(dt);
        }
    },
});
